"use strict";

// go controller
app.controller("GoController", ["$scope", "$http", "$state", "$translate", "$stateParams", "AuthService", "RESTService", "$location",
    function ($scope, $http, $state, $translate, $stateParams, AuthService, RESTService, $location) {
        $scope.init = function () {
            var token = $location.search().access_token;
            var sref = $location.search().sref;
            $scope.showimg = true;

            if (token != null) {
                AuthService.loginUsingAccessToken(token)
                    .then(function (data) {
                        // login success
                        $scope.setCurrentUser(AuthService.getLoginfo());

                        // check again
                        token = RESTService.getAccess_Token();
                        if ($location.search().params != null && $location.search().params != "") {
                            var params = JSON.parse($location.search().params);
                            $state.go(sref, params);
                        }else{
                            $state.go(sref,{});
                        }
                    }, function errorCallback(response) {
                        // login failure
                        $scope.showimg = false;
                    }
                );
            }
            else {
                $scope.showimg = false;
            }
        }

    }]);
